package com.dingding.action;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSONObject;
import com.dingding.bean.databean.request.DataRequest;
import com.dingding.bean.databean.response.DataResponse;
import com.dingding.bean.databean.response.ResUserLogin;
import com.dingding.common.core.config.SystemCode;
import com.dingding.common.core.util.DateFormatUtil;
import com.dingding.common.core.util.JSONUtil;
import com.dingding.common.core.util.ToolUtil;

/**
 * @ClassName: RequestDispatch
 * @Description: 请求模块调度代理
 * @author cheshaowei
 * @date 2015-9-17 下午09:18:14
 */
public class RequestDispatchProxy {

	private static final Logger log = Logger.getLogger(RequestDispatchProxy.class);

	RequestDispatch requestDispatch;// 请求模块调度

	/**
	 * 
	 * @Title: modularExcute
	 * @Description:模块调用
	 * @author cheshaowei
	 * 
	 */
	public String modularExcute(String requestData) {

		DataResponse dataResponse = new DataResponse();
		// TODO 待删除
		log.info("请求模块：" + JSONUtil.parseObject(requestData, DataRequest.class).getModular());

		if (ToolUtil.isBlank(requestData)) {

			log.error("从客户端获取的数据为空，服务器不做任何处理！");
			dataResponse.setResponseCode(SystemCode.DATA_NULL_ERROR);
			dataResponse.setResponseMessage(SystemCode.DATA_NULL_MESSAGE);

		} else {
			// 调用代理类执行请求的模块
			proxy(requestData, dataResponse);
		}

		String requestJsonData = null;

		// 登录接口需要单独设置时间格式
		if (dataResponse.getResponseBody() instanceof ResUserLogin) {

			requestJsonData = JSONUtil.toJSONString(dataResponse, "HH:mm");

		} else if (dataResponse.getResponseNumber() == "HH:mm") {
			requestJsonData = JSONUtil.toJSONString(dataResponse, "HH:mm");
		} else {

			requestJsonData = JSONUtil.toJSONString(dataResponse, "yyyy-MM-dd HH:mm:ss");

		}

		// if (dataResponse.getResponseBody() instanceof ResUserRouteList) {
		//
		// requestJsonData = JSONUtil.toJSONString(dataResponse, "HH:mm");
		//
		// } else {
		//
		// requestJsonData = JSONUtil.toJSONString(dataResponse, "yyyy-MM-dd
		// HH:mm:ss");
		//
		// }

		// TODO 待删除
		log.info("响应：" + dataResponse.getResponseMessage());
		if (dataResponse.getResponseBody() != null) {
			log.info("///////////响应内容：" + dataResponse.getResponseBody().toString());
		}

		return requestJsonData;

	}

	/**
	 * 
	 * @Title: proxy
	 * @Description: 代理类（用来动态调用客户端请求的模块）
	 * @param requestData
	 * @param dataResponse
	 *            设定文件
	 * @return void 返回类型
	 * 
	 */
	private void proxy(String requestData, DataResponse dataResponse) {

		JSONObject jsonObject = JSONUtil.parseObject(requestData);
		// 获取要请求的模块
		String modular = jsonObject.getString("modular");

		// // 获取请求序号
		// String requestNumber = jsonObject.getString("requestNumber");
		// // 获取手机IMEI
		// String imei = jsonObject.getString("imei");
		// // 获取手机
		// String appId = jsonObject.getString("appId");

		// 获取客户端请求的数据体
		String requestBody = jsonObject.getString("requestBody");
		
		log.info("请求内容：" + requestBody);
		
		// 通过反射获取类中的方法
		Method sayHelloMethod = null;
		try {

			sayHelloMethod = RequestDispatch.class.getDeclaredMethod(modular, String.class, DataResponse.class);

		} catch (NoSuchMethodException e) {
			log.error("客户端请客模块不存在！请求时间：" + DateFormatUtil.yyyy_MM_dd_HHmmss() + "请求模块：" + modular);
			// 请求模块不在
			dataResponse.setResponseCode(SystemCode.MODULAR_NOTEXIT_ERROR);
			dataResponse.setResponseMessage(SystemCode.MODULAR_NOTEXIT_MESSAGE);
		}
		// 调用方法
		try {
			sayHelloMethod.invoke(requestDispatch, requestBody, dataResponse);
		} catch (IllegalAccessException e) {
			log.error("动态调用了private方法！", e);
			setError(dataResponse);
		} catch (IllegalArgumentException e) {
			log.error("动态调用方法时传入参数不合法！", e);
			setError(dataResponse);
		} catch (InvocationTargetException e) {
			log.error("动态调用方法出现异常！", e);
			setError(dataResponse);
		}

	}

	private void setError(DataResponse dataResponse) {
		// 请求模块不在
		dataResponse.setResponseCode(SystemCode.ERROR);
		dataResponse.setResponseMessage(SystemCode.ERROR_MESSAGE);
	}

	public RequestDispatch getRequestDispatch() {
		return requestDispatch;
	}

	public void setRequestDispatch(RequestDispatch requestDispatch) {
		this.requestDispatch = requestDispatch;
	}

}
